/**
 * 
 * Copyright (C) 2015 by Elcom Software Company
 *
 * @id: JobSchedule.java, Mar 13, 2015
 *
 */
package com.nip.commons.job.objects;

import java.util.Date;

import com.nip.commons.job.exceptions.JobScheduleException;
import com.nip.commons.job.interf.IJobSchedule;

/**
 * @author ThangNM<thangnm@elcom.com.vn>
 * 
 * @version v
 */
public class JobSchedule implements IJobSchedule {

    protected String format_parterm = "[0-9]((,?(([0-9]{1,2}->[1-9]{1,2})|[0-9]{1,})){1,})?";
    private final String JOB_SCHEDULE_DEFAULT7 = "_____________Minute of hour (0-59) format mutils 0,7,9,.. ior * is minutely";
    private final String JOB_SCHEDULE_DEFAULT6 = "| ___________Hour of day (0-23) format mutils 0,4,5.. or * is hourly";
    private final String JOB_SCHEDULE_DEFAULT5 = "| | _________Day of month (0-31) format mutils 1,2,3,.. or * is daily";
    private final String JOB_SCHEDULE_DEFAULT4 = "| | | _______Month of year (1-12) format mutils 1,2,3,.. or * is monthly";
    private final String JOB_SCHEDULE_DEFAULT3 = "| | | | _____Year format mutils 1991,2000,... or * is yearly";
    private final String JOB_SCHEDULE_DEFAULT2 = "| | | | | __ Day of week (0-6) format mutil day 0,1,2 or * is daily";
    private final String JOB_SCHEDULE_DEFAULT1 = "| | | | | |";
    private final String JOB_SCHEDULE_DEFAULT0 = "* * * * * * schedule format";
    private TimeObject minute;
    private TimeObject hour;
    private TimeObject day;
    private TimeObject month;
    private TimeObject year;
    private TimeObject dayofweek;
    private Date startDate;

    /*
     * _____________Minute of hour (0-59) format mutils 0,7,9,.. ior * is minutely
     * | ___________Hour of day (0-23) format mutils 0,4,5.. or * is hourly
     * | | _________Day of month (0-31) format mutils 1,2,3,.. or * is daily
     * | | | _______Month of year (1-12) format mutils 1,2,3,.. or * is monthly
     * | | | | _____Year format mutils 1991,2000,... or * is yearly
     * | | | | | __ Day of week (0-6) format mutil day 0,1,2 or * is daily
     * | | | | | |
     * * * * * * * schedule format
     * 
     * 
     * @see com.nip.commons.job.interf.IJobSchedule#setShedule(java.lang.String)
     */
    @Override
    public void setShedule(String schedule_format) throws JobScheduleException {

        if (schedule_format == null)
            throw new JobScheduleException("schedule string is null");

        String[] schedule = schedule_format.split(" ");

        if (schedule == null || schedule.length != 6)
            throw new JobScheduleException("schedule format error.\n"
                    + JOB_SCHEDULE_DEFAULT0 + "\n" + JOB_SCHEDULE_DEFAULT1
                    + "\n" + JOB_SCHEDULE_DEFAULT2 + "\n"
                    + JOB_SCHEDULE_DEFAULT3 + "\n" + JOB_SCHEDULE_DEFAULT4
                    + "\n" + JOB_SCHEDULE_DEFAULT5 + "\n"
                    + JOB_SCHEDULE_DEFAULT6 + "\n" + JOB_SCHEDULE_DEFAULT7);
        int i = 0;
        boolean isAlways = false;
        for (String s : schedule) {
            isAlways = s.equals("*");
            if (!isAlways) {
                if (!s.trim().matches(format_parterm))
                    throw new JobScheduleException("schedule format error.\n"
                            + JOB_SCHEDULE_DEFAULT0 + "\n"
                            + JOB_SCHEDULE_DEFAULT1 + "\n"
                            + JOB_SCHEDULE_DEFAULT2 + "\n"
                            + JOB_SCHEDULE_DEFAULT3 + "\n"
                            + JOB_SCHEDULE_DEFAULT4 + "\n"
                            + JOB_SCHEDULE_DEFAULT5 + "\n"
                            + JOB_SCHEDULE_DEFAULT6 + "\n"
                            + JOB_SCHEDULE_DEFAULT7);
            }

            switch (i++) {
            case 0:
                minute = new TimeObject(s.split(","), isAlways);
                break;
            case 1:
                hour = new TimeObject(s.split(","), isAlways);
                break;
            case 2:
                day = new TimeObject(s.split(","), isAlways);
                break;
            case 3:
                month = new TimeObject(s.split(","), isAlways);
                break;
            case 4:
                year = new TimeObject(s.split(","), isAlways);
                break;
            case 5:
                dayofweek = new TimeObject(s.split(","), isAlways);
                break;
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.nip.commons.job.interf.IJobSchedule#getYear()
     */
    @Override
    public TimeObject getYear() {
        // TODO Auto-generated method stub
        return year;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.nip.commons.job.interf.IJobSchedule#getMonth()
     */
    @Override
    public TimeObject getMonth() {
        // TODO Auto-generated method stub
        return month;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.nip.commons.job.interf.IJobSchedule#getHour()
     */
    @Override
    public TimeObject getHour() {
        // TODO Auto-generated method stub
        return hour;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.nip.commons.job.interf.IJobSchedule#getMinute()
     */
    @Override
    public TimeObject getMinute() {
        // TODO Auto-generated method stub
        return minute;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.nip.commons.job.interf.IJobSchedule#getDayOfWeek()
     */
    @Override
    public TimeObject getDayOfWeek() {
        // TODO Auto-generated method stub
        return dayofweek;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.nip.commons.job.interf.IJobSchedule#getDay()
     */
    @Override
    public TimeObject getDay() {
        // TODO Auto-generated method stub
        return day;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.nip.commons.job.interf.IJobSchedule#getStartDate()
     */
    @Override
    public Date getStartDate() {
        // TODO Auto-generated method stub
        return this.startDate;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.nip.commons.job.interf.IJobSchedule#setStartDate(java.util.Date)
     */
    @Override
    public void setStartDate(Date date) {
        this.startDate = date;
    }

}
